/**
 * $Id: SGIPSubmitMessage.java,v 1.2 2006/09/13 05:38:27 martin Exp $
 *
 * Copyright 2005 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.sgip12msg;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;

public class SGIPSubmitMessage extends SGIPMessage {

    private String outStr;
    public SGIPSubmitMessage(String SPNumber, String ChargeNumber,
            String UserNumber[], String CorpId, String ServiceType,
            int FeeType, String FeeValue, String GivenValue, int AgentFlag,
            int MorelatetoMTFlag, int Priority, Date ExpireTime,
            Date ScheduleTime, int ReportFlag, int TP_pid, int TP_udhi,
            int MessageCoding, int MessageType, int MessageLen,
            byte MessageContent[], String reserve)
            throws IllegalArgumentException {
        if (SPNumber.length() > 21)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:SPNumber length is larger than 21");
        if (ChargeNumber.length() > 21)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:ChargeNumber length is larger than 21");
        if (UserNumber.length > 100)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:UserCount length is larger than 100");
        for (int i = 0; i < UserNumber.length; i++) {
            if (UserNumber[i].length() > 21) {
                throw new IllegalArgumentException(
                        "The parameter of submit message is wrong:UserNumber["
                                + i + "] length is larger than 21");
            }
        }
        if (CorpId.length() > 5) {
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:CorpId length is larger than 5");
        }
        if (ServiceType.length() > 10)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:ServiceType length is larger than 10");
        if (FeeType < 0 || FeeType > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:FeeType less than 0 or larger than 255.");
        if (FeeValue.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:FeeValue length is larger than 6");

        if (GivenValue.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:GivenValue length is larger than 6");

        if (AgentFlag < 0 || AgentFlag > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:AgentFlag less than 0 or larger than 255.");
        if (MorelatetoMTFlag < 0 || MorelatetoMTFlag > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MorelatetoMTFlag less than 0 or larger than 255.");
        if (Priority < 0 || Priority > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:Priority less than 0 or larger than 255.");
        if (ReportFlag < 0 || ReportFlag > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:ReportFlag less than 0 or larger than 255.");
        if (TP_pid < 0 || TP_pid > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:TP_pid less than 0 or larger than 255.");
        if (TP_udhi < 0 || TP_udhi > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:TP_udhi less than 0 or larger than 255.");
        if (MessageCoding < 0 || MessageCoding > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MessageCoding less than 0 or larger than 255.");
        if (MessageType < 0 || MessageType > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MessageType less than 0 or larger than 255.");
        if (MessageLen < 0 || MessageLen > 160)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MessageLen less than 0 or larger than 160");
        if (MessageContent.length > 160)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MessageContent length is larger than 160");
        if (reserve.length() > 8)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MessageContent length is larger than 8");
        int len = 143 + 21 * UserNumber.length + MessageLen;
       
        buf = new byte[len];
        TypeConvert.int2byte(len, buf, 0);
        TypeConvert.int2byte(SGIPCommon.SGIP_SUBMIT, buf, 4);

        System.arraycopy(SPNumber.getBytes(), 0, buf, 20, SPNumber.length());

        System.arraycopy(ChargeNumber.getBytes(), 0, buf, 41, ChargeNumber
                .length());

        buf[62] = (byte) UserNumber.length;
        int i = 0;

        for (i = 0; i < UserNumber.length; i++)
            System.arraycopy(UserNumber[i].getBytes(), 0, buf, 63 + i * 21,
                    UserNumber[i].length());

        int loc = 63 + i * 21;

        System.arraycopy(CorpId.getBytes(), 0, buf, loc, CorpId.length());
        loc += 5;

        System.arraycopy(ServiceType.getBytes(), 0, buf, loc, ServiceType
                .length());
        loc += 10;
        buf[loc++] = (byte) FeeType;

        System.arraycopy(FeeValue.getBytes(), 0, buf, loc, FeeValue.length());
        loc += 6;

        System.arraycopy(GivenValue.getBytes(), 0, buf, loc, GivenValue
                .length());

        loc += 6;
        buf[loc++] = (byte) AgentFlag;
        buf[loc++] = (byte) MorelatetoMTFlag;
        buf[loc++] = (byte) Priority;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        if (ExpireTime != null) {
            String tmpTime = String.valueOf(
                    String.valueOf(dateFormat.format(ExpireTime))).concat(
                    "032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, loc, 16);
        }

        loc += 16;
        if (ScheduleTime != null) {
            String tmpTime = String.valueOf(
                    String.valueOf(dateFormat.format(ScheduleTime))).concat(
                    "032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, loc, 16);
        }

        loc += 16;
        buf[loc++] = (byte) ReportFlag;
        buf[loc++] = (byte) TP_pid;
        buf[loc++] = (byte) TP_udhi;
        buf[loc++] = (byte) MessageCoding;
        buf[loc++] = (byte) MessageType;
        TypeConvert.int2byte(MessageLen, buf, loc);
        loc += 4;
        System.arraycopy(MessageContent, 0, buf, loc, MessageLen);
        loc += MessageLen;
        System.arraycopy(reserve.getBytes(), 0, buf, loc, reserve.length());
        StringBuffer outBuff = new StringBuffer(",SPNumber=").append(SPNumber)
                .append(",ChargeNumber=").append(ChargeNumber).append(
                        ",UserCount=").append(UserNumber.length);

        for (int t = 0; t < UserNumber.length; t++)
            outBuff.append(",UserNumber[").append(t).append("]=").append(
                    UserNumber[t]);

        outBuff.append(",CorpId=").append(CorpId).append(",ServiceType=")
                .append(ServiceType).append(",FeeType=").append(FeeType)
                .append(",FeeValue=").append(FeeValue).append(",GivenValue=")
                .append(GivenValue).append(",AgentFlag=").append(AgentFlag)
                .append(",MorelatetoMTFlag=").append(MorelatetoMTFlag).append(
                        ",Priority=").append(Priority);
        if (ExpireTime != null)
            outBuff.append(",ExpireTime=")
                    .append(dateFormat.format(ExpireTime));
        else
            outBuff.append(",ExpireTime=null");
        if (ScheduleTime != null)
            outBuff.append(",ScheduleTime=").append(
                    dateFormat.format(ScheduleTime));
        else
            outBuff.append(",ScheduleTime=null");
        outBuff.append(",ReportFlag=").append(ReportFlag).append(",TP_pid=")
                .append(TP_pid).append(",TP_udhi=").append(TP_udhi).append(
                        ",MessageCoding=").append(MessageCoding).append(
                        ",MessageType=").append(MessageType).append(
                        ",MessageLength=").append(MessageLen).append(
                        ",MessageContent=").append(new String(MessageContent))
                .append(",reserve=").append(reserve);
        outStr = outBuff.toString();
    }

    public String toString() {
        String tmpStr = new StringBuffer("SGIP_Submit: ")
                .append("Src_node_Id=").append(getSrcNodeId()).append(
                        ",Time_Stamp=").append(getTimeStamp()).append(
                        ",Sequence_Id=").append(getSequenceId()).append(outStr)
                .toString();
        return tmpStr;
    }

    public int getCommandId() {
        return SGIPCommon.SGIP_SUBMIT;
    }
}
